package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.BasePageRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.controller.request.UserPageRequest;
import com.example.springboot.dto.AdminLoginDTO;
import com.example.springboot.dto.UserLoginDTO;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TimerTask.TaskInfo;
import com.example.springboot.utils.TimerTask.TaskType;
import com.example.springboot.utils.TimerTask.TimerHashMap;
import com.example.springboot.utils.TimerTask.TimerTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:UserController
 * Package:com.example.springboot.controller
 * Description:
 *
 * @Author: Gao
 * @Create: 2023/2/1 - 16:34
 */
@RestController
@RequestMapping( "/user")
public class UserController {

    @Autowired
    UserService userService;

    @Resource
    private TimerTask timerTask;

    /**
     * 获取所有用户
     * @return
     */
    @GetMapping("/")
    public Result list(){
        List<User> users = userService.list();
        return Result.success(users);
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") int id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 分页
     * @param userPageRequest
     * @return
     * @return
     */
    @GetMapping("/page")
    public Result page(UserPageRequest userPageRequest){
        Object page = userService.page(userPageRequest);
        return Result.success(page);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/")
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id){
        userService.deleteById(id);
        return Result.success();
    }
/*------------------------------以下设置了token校验，但是前端没有用户端暂时保留不测---------------------------------------------------------------*/
    /**
     * 修改用户密码
     * @param
     * @return
     */
    @PutMapping("/password")
    public Result password(@RequestBody PasswordRequest passwordRequest){
        boolean taskIsEnd = timerTask.taskIsEnd(passwordRequest.getId().toString());
        if (taskIsEnd)
            return Result.error("token过期！拒绝访问接口");
        userService.changePassword(passwordRequest);
        return Result.success();
    }

    /**
     * 用户登录
     * @param request
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request){
        UserLoginDTO loginDTO = userService.login(request);
        /*这里设置的token过时时间是3秒钟*/
        String token = loginDTO.getId().toString();
        timerTask.setTimer(token,new TaskInfo(loginDTO.getToken(),3000, TaskType.TASK_DELETE_CACHE));
        return Result.success(loginDTO);
    }

}
